package com.bunnyhsu.controller;

import com.bunnyhsu.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bunnyhsu.model.User;

import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2017/6/21.
 */
@Controller
public class HelloController {

    Logger logger = Logger.getLogger(HelloController.class);

    private final UserService userService;

    @Autowired
    public HelloController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/test/{value}")
    public String hello(@PathVariable("value") String value)
    {
        return "404";
    }

    @RequestMapping(value = "/db/{value}")
    public String test(@PathVariable("value") String value, Map<String, Object> map)
    {
        switch (value)
        {
            case "name":
                User user = userService.selectUser(1);
                map.put("user",user.getUser_name());
                map.put("target","names");
                return "index";
            case "names":
                List<User> users = userService.selectAll();
                StringBuffer str = new StringBuffer("");
                for (User user_of_users : users)
                {
                    str.append(user_of_users.getUser_name());
                    str.append("-");
                }
                str.deleteCharAt(str.length()-1);
                map.put("user",str.toString());
                map.put("target","name");
                return "index";
            default:
                return "404";
        }
    }
}
